package cn.lee.cplibrary.widget.picker.util;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import cn.lee.cplibrary.R;
import cn.lee.cplibrary.util.LogUtil;
import cn.lee.cplibrary.util.dialog.CpComDialog;
import cn.lee.cplibrary.util.ObjectUtils;
import cn.lee.cplibrary.util.ScreenUtil;
import cn.lee.cplibrary.widget.picker.adapter.NumericWheelAdapter;
import cn.lee.cplibrary.widget.picker.widget.OnWheelChangedListener;
import cn.lee.cplibrary.widget.picker.widget.WheelView;


/**
 * @author ChrisLee
 */
public class DatePickerUtils {

    private static WheelView year, month, day, mins, hour;
    private int visibleItemNum = 5;
    private boolean isCyclic;

    private int curYear = Calendar.getInstance().get(Calendar.YEAR);//日期当前年
    private int curMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;//日期当前月
    private int curDay = Calendar.getInstance().get(Calendar.DATE);//日期当前天
    //    private  int curHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);//当前小时---经测试，此时获取，永远是0
//    private  int curMin = Calendar.getInstance().get(Calendar.MINUTE);//当前分钟---经测试，此时获取，永远是0
    private int curHour = 0;//当前小时
    private int curMin = 0;//当前分钟
    //ui设置
    private Context context;
    private int tBgColor;
    private int tTxtColor;
    private int tTxtSize; //单位 sp ，默认值是7，相当于布局中的14sp
    private String tTitle;
    private boolean isShowLabel;
    private boolean maxIsNow;//当前时间是否是控件能选择的最大时间
    private boolean minIsNow;//当前时间是否是控件能选择的最小时间
    private static int minYearInit = 1900, maxYearInit = 2100;//理论上的值
    private int minYearReal = minYearInit, maxYearReal = maxYearInit;//年-实际的值
    private int minMonthReal = 1, maxMonthReal = 12;//月-实际的值
    private int minDayReal = 1, maxDayReal = 31;//日-实际的值
    private int minHourReal = 0, maxHourReal = 23;//时-实际的值
    private int minMinReal = 0, maxMinReal = 59;//分-实际的值

    private DatePickerUtils(Context context) {
        this.context = context;
    }

    /**
     * 显示年月
     */
    public void showMonth(
            final MonthCallBack callBack) {
        View view = LayoutInflater.from(context).inflate(R.layout.cp_date_time_picker_layout, null);
        final Dialog dialog = CpComDialog.getBottomDialog(context, true, view);
        setView(view);
        view.findViewById(R.id.new_day).setVisibility(View.GONE);
        view.findViewById(R.id.new_hour).setVisibility(View.GONE);
        view.findViewById(R.id.new_mins).setVisibility(View.GONE);
        // 设置年月日的最大值和最小值、设置当前时间
        //--------------------年--------------------
        year = view.findViewById(R.id.new_year);
        initYear(context);
        year.setCurrentItem(curYear - minYearReal);
        //--------------------月--------------------
        month = view.findViewById(R.id.new_month);
        initMonth(context, null);
        month.setCurrentItem(curMonth - minMonthReal);

        year.setVisibleItems(visibleItemNum);
        month.setVisibleItems(visibleItemNum);
        // 设置监听
        view.findViewById(R.id.set).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.sure(year.getCurrentItem() + minYearReal,
                        month.getCurrentItem() + minMonthReal);
                dialog.cancel();
            }
        });
        view.findViewById(R.id.cancel).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.cancel();
                dialog.cancel();
            }
        });
        addListenerYearForMonth();
    }


    /**
     * 显示年月日
     */
    public void showDate(
            final DateCallBack callBack) {
        View view = LayoutInflater.from(context).inflate(R.layout.cp_date_time_picker_layout, null);
        final Dialog dialog = CpComDialog.getBottomDialog(context, true, view);
        setView(view);
        view.findViewById(R.id.new_hour).setVisibility(View.GONE);
        view.findViewById(R.id.new_mins).setVisibility(View.GONE);
        // 设置年月日的最大值和最小值、设置当前时间
        //--------------------年--------------------
        year = view.findViewById(R.id.new_year);
        initYear(context);
        year.setCurrentItem(curYear - minYearReal);
        //--------------------月--------------------
        month = view.findViewById(R.id.new_month);
        initMonth(context, null);
        month.setCurrentItem(curMonth - minMonthReal);
        //--------------------日--------------------
        day = view.findViewById(R.id.new_day);
        initDay(context, year, month);
        day.setCurrentItem(curDay - minDayReal);

        year.setVisibleItems(visibleItemNum);
        month.setVisibleItems(visibleItemNum);
        day.setVisibleItems(visibleItemNum);
        // 设置监听
        view.findViewById(R.id.set).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.sure(year.getCurrentItem() + minYearReal,
                        month.getCurrentItem() + minMonthReal,
                        day.getCurrentItem() + minDayReal);
                dialog.cancel();
            }
        });
        view.findViewById(R.id.cancel).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.cancel();
                dialog.cancel();
            }
        });
        addListenerYearForMonth();
        addListenerYearMonthForDay();
    }

    /**
     * 显示全部日期
     */
    public void showDateAndTime(
            final DateAndTimeCallBack callBack) {
        View view = LayoutInflater.from(context).inflate(R.layout.cp_date_time_picker_layout, null);
        final Dialog dialog = CpComDialog.getBottomDialog(context, true, view);
        setView(view);

        // 设置年月日的最大值和最小值、设置当前时间
        //--------------------年--------------------
        year = view.findViewById(R.id.new_year);
        initYear(context);
        year.setCurrentItem(curYear - minYearReal);
        //--------------------月--------------------
        month = view.findViewById(R.id.new_month);
        initMonth(context, null);
        month.setCurrentItem(curMonth - minMonthReal);
        //--------------------日--------------------
        day = view.findViewById(R.id.new_day);
        initDay(context, year, month);
        day.setCurrentItem(curDay - minDayReal);
        //--------------------时--------------------
        hour = (WheelView) view.findViewById(R.id.new_hour);
        initHour(context);
        curHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);//当前小时
        hour.setCurrentItem(curHour - minHourReal);
        //--------------------分--------------------
        mins = (WheelView) view.findViewById(R.id.new_mins);
        initMins(context);
        curMin = Calendar.getInstance().get(Calendar.MINUTE);//当前分钟
        mins.setCurrentItem(curMin - minMinReal);

        year.setVisibleItems(visibleItemNum);
        month.setVisibleItems(visibleItemNum);
        day.setVisibleItems(visibleItemNum);
        hour.setVisibleItems(visibleItemNum);
        mins.setVisibleItems(visibleItemNum);
        // 设置监听
        TextView ok = (TextView) view.findViewById(R.id.set);
        TextView cancel = (TextView) view.findViewById(R.id.cancel);
        ok.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.sure(year.getCurrentItem() + minYearReal,
                        month.getCurrentItem() + minMonthReal,
                        day.getCurrentItem() + minDayReal,
                        hour.getCurrentItem()+ minHourReal,
                        mins.getCurrentItem()+minMinReal);
                dialog.cancel();
            }
        });
        cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.cancel();
                dialog.cancel();
            }
        });

        addListenerYearForMonth();
        addListenerYearMonthForDay();
        addListenerForHourMin();
     }

    /**
     * 显示小时、分钟
     */
    public void showHourMin(final HourMinCallBack callBack) {
        View view = LayoutInflater.from(context).inflate(R.layout.cp_date_time_picker_layout, null);
        final Dialog dialog = CpComDialog.getBottomDialog(context, true, view);
        setView(view);
        view.findViewById(R.id.new_year).setVisibility(View.GONE);
        view.findViewById(R.id.new_month).setVisibility(View.GONE);
        view.findViewById(R.id.new_day).setVisibility(View.GONE);
        year = view.findViewById(R.id.new_year);
        month = view.findViewById(R.id.new_month);
        day = view.findViewById(R.id.new_day);
        hour = view.findViewById(R.id.new_hour);
        initHour(context);
        mins = view.findViewById(R.id.new_mins);
        initMins(context);
        curHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);//当前小时
        curMin = Calendar.getInstance().get(Calendar.MINUTE);//当前分钟
        // 设置当前时间
        hour.setCurrentItem(curHour);
        mins.setCurrentItem(curMin);
        hour.setVisibleItems(visibleItemNum);
        mins.setVisibleItems(visibleItemNum);
        // 设置监听
        TextView ok = view.findViewById(R.id.set);
        TextView cancel = view.findViewById(R.id.cancel);
        ok.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.sure(hour.getCurrentItem(), mins.getCurrentItem());
                dialog.cancel();
            }
        });
        cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.cancel();
                dialog.cancel();
            }
        });
    }

    /**
     * 设置对话框外观
     */
    private void setView(View titleBar, TextView tvTitle, TextView tvLeftBtn, TextView tvRightBtn) {
        titleBar.setBackgroundColor(tBgColor);
        if (!ObjectUtils.isEmpty(tTitle)) {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(tTitle);
            tvTitle.setTextColor(tTxtColor);
            tvTitle.setTextSize(ScreenUtil.sp(context, tTxtSize));
        } else {
            tvTitle.setVisibility(View.GONE);
        }
        tvLeftBtn.setTextColor(tTxtColor);
        tvRightBtn.setTextColor(tTxtColor);
        tvLeftBtn.setTextSize(ScreenUtil.sp(context, tTxtSize));
        tvRightBtn.setTextSize(ScreenUtil.sp(context, tTxtSize));
    }

    /**
     * 设置对话框外观
     */
    private void setView(View layout) {
        View titleBar = layout.findViewById(R.id.rl_title);
        TextView tvTitle = layout.findViewById(R.id.tv_title);
        TextView tvLeftBtn = layout.findViewById(R.id.cancel);
        TextView tvRightBtn = layout.findViewById(R.id.set);
        setView(titleBar, tvTitle, tvLeftBtn, tvRightBtn);
    }

    private void updateDays(Context context, WheelView year,
                            WheelView month, WheelView day) {
        String label = isShowLabel ? " 日" : "";
        minDayReal = getShowMinDay(year, month);
        maxDayReal = getShowMaxDay(year, month);
        NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(
                context, minDayReal, maxDayReal, "%02d");
        numericWheelAdapter.setLabel(label);
        // numericWheelAdapter.setTextSize(15); 设置字体大小
        day.setViewAdapter(numericWheelAdapter);
        day.setCyclic(isCyclic);
//        day.setCurrentItem(curDay-minDayReal, true);//显示当前天数
        day.setCurrentItem(0, true);
//        int curDay = Math.min(maxDayReal, day.getCurrentItem() + minDayReal);
//        day.setCurrentItem(curDay - 1, true);
    }

    private int updateMonth(Context context, WheelView year) {
        String label = isShowLabel ? " 月" : "";
        minMonthReal = getShowMinMonth(year);
        maxMonthReal = getShowMaxMonth(year);
        NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(
                context, minMonthReal, maxMonthReal, "%02d");
        numericWheelAdapter.setLabel(label);
        // numericWheelAdapter.setTextSize(15); 设置字体大小
        month.setViewAdapter(numericWheelAdapter);
        month.setCyclic(isCyclic);
//        month.setCurrentItem(curMonth-minMonthReal, true);//显示当前月
        month.setCurrentItem(0, true);
        return minMonthReal;
    }
    private int updateHour(Context context ) {
        initHour(context);
        hour.setCurrentItem(0, true);
        return minHourReal;
    }
    private int updateMin(Context context ) {
        initMins(context);
        mins.setCurrentItem(0, true);
        return minMinReal;
    }
    /**
     * 初始化年
     */
    private void initYear(Context context) {
        String label = isShowLabel ? " 年" : "";
        //计算最小值、最大值
        maxYearReal = maxIsNow ? curYear : maxYearReal;
        minYearReal = (!maxIsNow && minIsNow) ? curYear : minYearReal;
        NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(
                context, minYearReal, maxYearReal);
        numericWheelAdapter.setLabel(label);
        // numericWheelAdapter.setTextSize(15); 设置字体大小
        year.setViewAdapter(numericWheelAdapter);
        year.setCyclic(isCyclic);
    }


    /**
     * 初始化月
     *
     * @return 显示的最小月份
     */
    private int initMonth(Context context, WheelView year) {
        String label = isShowLabel ? " 月" : "";
        minMonthReal = getShowMinMonth(year);
        maxMonthReal = getShowMaxMonth(year);
        NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(
                context, minMonthReal, maxMonthReal, "%02d");
        numericWheelAdapter.setLabel(label);
        // numericWheelAdapter.setTextSize(15); 设置字体大小
        month.setViewAdapter(numericWheelAdapter);
        month.setCyclic(isCyclic);
        return minMonthReal;
    }

    /**
     * 初始化天
     *
     * @return 显示的最小天
     */
    private int initDay(Context context, WheelView year, WheelView month) {
        String label = isShowLabel ? " 日" : "";
        minDayReal = getShowMinDay(year, month);
        maxDayReal = getShowMaxDay(year, month);
        NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(
                context, minDayReal, maxDayReal, "%02d");
        numericWheelAdapter.setLabel(label);
        // numericWheelAdapter.setTextSize(15); 设置字体大小
        day.setViewAdapter(numericWheelAdapter);
        day.setCyclic(isCyclic);
        return minDayReal;
    }

    /**
     * 初始化时
     */
    private void initHour(Context context) {
        String label = isShowLabel ? " 时" : "";
        minHourReal = getShowMinHour();
        maxHourReal = getShowMaxHour();
        NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(
                context, minHourReal, maxHourReal, "%02d");
        numericWheelAdapter.setLabel(label);
        // numericWheelAdapter.setTextSize(15); 设置字体大小
        hour.setViewAdapter(numericWheelAdapter);
        hour.setCyclic(isCyclic);
    }

    /**
     * 初始化分
     */
    private void initMins(Context context) {
        String label = isShowLabel ? " 分" : "";
        minMinReal = getShowMinMin();
        maxMinReal = getShowMaxMin();
        NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(
                context, minMinReal, maxMinReal, "%02d");
        numericWheelAdapter.setLabel(label);
        // numericWheelAdapter.setTextSize(15); 设置字体大小
        mins.setViewAdapter(numericWheelAdapter);
        mins.setCyclic(isCyclic);
    }


    // 获取当月天数
    @SuppressLint("SimpleDateFormat")
    private static int getDays(int year, int month) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月");
        String source = String.valueOf(year) + "年" + String.valueOf(month)
                + "月";
        Date date = null;
        try {
            date = format.parse(source);
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取选择年份后应该显示的最大月份
     */
    private int getShowMaxMonth(WheelView year) {
        int maxMonth = maxIsNow ? curMonth : 12;
        if (year != null) {
            maxMonth = (maxIsNow && getSelectYear(year) == curYear) ? curMonth : 12;
        }
        return maxMonth;
    }

    /**
     * 获取选择年份后应该显示的最小月份
     */
    private int getShowMinMonth(WheelView year) {
        int minMonth = (!maxIsNow && minIsNow) ? curMonth : 1;
        if (year != null) {
            minMonth = ((!maxIsNow && minIsNow) && getSelectYear(year) == curYear) ? curMonth : 1;
        }
        return minMonth;
    }

    /**
     * 获取选择年月后应该显示的最“大”天数
     */
    private int getShowMaxDay(WheelView year, WheelView month) {
        int yearSelect = getSelectYear(year);
        int monthSelect = getSelectMonth(month);
        int daysInit = getDays(yearSelect, monthSelect);//按照日期当前年月的天数
        int maxDay = maxIsNow ? curDay : daysInit;
        if (year != null && month != null) {//设置目前日期是最大日期并且显示的是当前年月
            maxDay = (maxIsNow && yearSelect == curYear && monthSelect == curMonth) ? curDay : daysInit;
        }
        return maxDay;
    }

    /**
     * 获取选择年月后应该显示的最“小”天数
     */
    private int getShowMinDay(WheelView year, WheelView month) {
        int yearSelect = getSelectYear(year);
        int monthSelect = getSelectMonth(month);
        int minDay = (!maxIsNow && minIsNow) ? curDay : 1;
        if (year != null && month != null) {
            minDay = ((!maxIsNow && minIsNow)
                    && yearSelect == curYear
                    && monthSelect == curMonth) ? curDay : 1;
        }
        return minDay;
    }

    /**
     * 获取显示的最大小时（当年月日已经选定）
     */
    private int getShowMaxHour() {
        int yearSelect = getSelectYear(year);
        int monthSelect = getSelectMonth(month);
        int daySelect = getSelectDay(day);
        curHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        int maxHour = (maxIsNow
                && yearSelect == curYear
                && monthSelect == curMonth
                && daySelect == curDay) ? curHour : 23;

        return maxHour;
    }

    /**
     * 获取显示的最小小时（当年月日已经选定）
     */
    private int getShowMinHour() {
        int yearSelect = getSelectYear(year);
        int monthSelect = getSelectMonth(month);
        int daySelect = getSelectDay(day);
        curHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        int minHour = ((!maxIsNow && minIsNow)
                && yearSelect == curYear
                && monthSelect == curMonth
                && daySelect == curDay) ? curHour : 0;

        return minHour;
    }

    /**
     * 获取显示的最大分钟（当年月日时已经选定）
     */
    private int getShowMaxMin() {
        int yearSelect = getSelectYear(year);
        int monthSelect = getSelectMonth(month);
        int daySelect = getSelectDay(day);
        int hourSelect = getSelectHour(hour);
        curHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        curMin = Calendar.getInstance().get(Calendar.MINUTE);
        int maxMin = (maxIsNow
                && yearSelect == curYear
                && monthSelect == curMonth
                && daySelect == curDay
                && hourSelect == curHour
        ) ? curMin : 59;

        return maxMin;
    }

    /**
     * 获取显示的最小分钟（当年月日时已经选定）
     */
    private int getShowMinMin() {
        int yearSelect = getSelectYear(year);
        int monthSelect = getSelectMonth(month);
        int daySelect = getSelectDay(day);
        int hourSelect = getSelectHour(hour);
        curHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        curMin = Calendar.getInstance().get(Calendar.MINUTE);
        int minMin = ((!maxIsNow && minIsNow)
                && yearSelect == curYear
                && monthSelect == curMonth
                && daySelect == curDay
                && hourSelect == curHour
        ) ? curMin : 0;
        return minMin;
    }

    /**
     * 获取当前选择的年
     */
    private int getSelectYear(WheelView year) {
        int y = year.getCurrentItem() + minYearReal;
        return y;
    }

    /**
     * 获取当前选择的月
     */
    private int getSelectMonth(WheelView month) {
        int m = month.getCurrentItem() + minMonthReal;
        return m;
    }

    /**
     * 获取当前选择的天
     */
    private int getSelectDay(WheelView day) {
        int d = day.getCurrentItem() + minDayReal;
        return d;
    }

    /**
     * 获取当前选择的时
     */
    private int getSelectHour(WheelView hour) {
        int h = hour.getCurrentItem() + minHourReal;
        return h;
    }

    /**
     * 增加年份滚动监听，改变一年中月份的多少
     */
    private void addListenerYearForMonth() {
        year.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                //设置了当前日期是截至或者开始日期-才需要更新
                int lastPoi = wheel.getViewAdapter().getItemsCount() - 1;
                if (maxIsNow) {
                    if (oldValue == lastPoi || newValue == lastPoi) {
                        updateMonth(context, year);
                        LogUtil.i("", "oldValue=" + oldValue + ",newValue=" + newValue);
                    }
                } else if (minIsNow && ((oldValue == 0 || newValue == 0))) {
                    updateMonth(context, year);
                    LogUtil.i("", "oldValue=" + oldValue + ",newValue=" + newValue);
                }
            }
        });
    }

    /**
     * 增加年份滚动监听，改变一年中月份的多少
     */
    private void addListenerYearMonthForDay() {
        OnWheelChangedListener listener = new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                updateDays(context, year, month, day);
            }
        };
        year.addChangingListener(listener);
        month.addChangingListener(listener);

    }
    /**
     * 改变小时和分钟
     */
    private void addListenerForHourMin() {
        OnWheelChangedListener listener = new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                //设置了当前日期是截至或者开始日期-才需要更新
                if (maxIsNow) {
                    int lastPoi = wheel.getViewAdapter().getItemsCount() - 1;
                    if (oldValue == lastPoi || newValue == lastPoi) {
                        updateHour(context);
                        updateMin(context);
                    }
                } else if (minIsNow && ((oldValue == 0 || newValue == 0))) {
                    updateHour(context);
                    updateMin(context);
                }
            }
        };
        OnWheelChangedListener listenerMin = new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                //设置了当前日期是截至或者开始日期-才需要更新
                if (maxIsNow) {
                    int lastPoi = wheel.getViewAdapter().getItemsCount() - 1;
                    if (oldValue == lastPoi || newValue == lastPoi) {
                        updateMin(context);
                    }
                } else if (minIsNow && ((oldValue == 0 || newValue == 0))) {
                    updateMin(context);
                }
                updateMin(context);
            }
        };
        year.addChangingListener(listener);
        month.addChangingListener(listener);
        day.addChangingListener(listener);
        hour.addChangingListener(listenerMin);
    }

    public interface DateAndTimeCallBack {
        void sure(int year, int month, int day, int hour, int min);
        void cancel();
    }

    public interface DateCallBack {
        void sure(int year, int month, int day);

        void cancel();
    }

    public interface MonthCallBack {
        void sure(int year, int month);

        void cancel();
    }

    public interface HourMinCallBack {
        void sure(int hour, int min);

        void cancel();
    }

    /**
     * @return :格式 例如 2018-02-09 05：30
     */
    public static String format(int year, int month, int day,
                                int hour, int min) {
        String dateAndTime = String.format(Locale.CHINA,
                "%04d-%02d-%02d %02d:%02d", year, month, day, hour, min);
        return dateAndTime;
    }

    /**
     * @return :格式 例如 2018-02-09
     */
    public static String format(int year, int month, int day) {
        String date = String.format(Locale.CHINA,
                "%04d-%02d-%02d", year, month, day);
        return date;
    }

    /**
     * @return :格式 例如 2018-02
     */
    public static String format(int year, int month) {
        String date = String.format(Locale.CHINA,
                "%04d-%02d", year, month);
        return date;
    }

    /**
     * @return :格式 例如 05：30
     */
    public static String formatHourMin(int hour, int min) {
        String dateAndTime = String.format(Locale.CHINA,
                "%02d:%02d", hour, min);
        return dateAndTime;
    }

    /**
     * @return :格式 例如 2018.02.09
     */
    public static String formatDateDot(int year, int month, int day) {
        String date = String.format(Locale.CHINA,
                "%04d.%02d.%02d", year, month, day);
        return date;
    }


    private void settBgColor(int tBgColor) {
        this.tBgColor = tBgColor;
    }


    private void settTxtColor(int tTxtColor) {
        this.tTxtColor = tTxtColor;
    }

    private void settTxtSize(int tTxtSize) {
        this.tTxtSize = tTxtSize;
    }

    private void settTitle(String tTitle) {
        this.tTitle = tTitle;
    }

    private void setShowLabel(boolean showLabel) {
        isShowLabel = showLabel;
    }

    public void setVisibleItemNum(int visibleItemNum) {
        this.visibleItemNum = visibleItemNum;
    }

    public void setCyclic(boolean cyclic) {
        isCyclic = cyclic;
    }

    public void setCurHour(int curHour) {
        this.curHour = curHour;
    }

    public void setCurMin(int curMin) {
        this.curMin = curMin;
    }

    public void setMaxIsNow(boolean maxIsNow) {
        this.maxIsNow = maxIsNow;
    }

    public void setMinIsNow(boolean minIsNow) {
        this.minIsNow = minIsNow;
    }

    public static class Builder {
        private Context context;
        private int tBgColor = Color.parseColor("#1086D1");//时间选择框标题栏背景色
        private int tTxtColor = Color.parseColor("#FFFFFF");//标题栏：文字颜色（确定、取消按钮、标题）
        private int tTxtSize = 14;//标题栏：文字大小（确定、取消按钮、标题） 单位sp
        private String tTitle;//标题栏：标题文字
        private boolean isShowLabel = true;//时间控件是否显示label 年月日等
        private boolean isCyclic;//数据是否循环显示
        private boolean maxIsNow;//当前时间是否是控件能选择的最大时间
        private boolean minIsNow;//当前时间是否是控件能选择的最小时间
        private int visibleItemNum = 5;
        private int curHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);//当前小时
        private int curMin = Calendar.getInstance().get(Calendar.MINUTE);//当前分钟

        private Builder(Context context) {
            this.context = context;
        }

        public static Builder builder(Context context) {
            return new Builder(context);
        }

        public DatePickerUtils build() {
            DatePickerUtils util = new DatePickerUtils(context);
            util.settBgColor(tBgColor);
            util.settTxtColor(tTxtColor);
            util.settTxtSize(tTxtSize);
            util.settTitle(tTitle);
            util.setShowLabel(isShowLabel);
            util.setCyclic(isCyclic);
            util.setVisibleItemNum(visibleItemNum);
            util.setCurHour(curHour);
            util.setCurMin(curMin);
            util.setMaxIsNow(maxIsNow);
            util.setMinIsNow(minIsNow);
            return util;
        }

        public Builder settBgColor(int tBgColor) {
            this.tBgColor = tBgColor;
            return this;
        }

        public Builder settTxtColor(int tTxtColor) {
            this.tTxtColor = tTxtColor;
            return this;
        }

        /**
         * @param tTxtSize 单位 sp ，默认值是7，相当于布局中的14sp
         */
        public Builder settTxtSize(int tTxtSize) {
            this.tTxtSize = tTxtSize;
            return this;
        }

        public Builder settTitle(String tTitle) {
            this.tTitle = tTitle;
            return this;
        }

        public Builder setShowLabel(boolean showLabel) {
            isShowLabel = showLabel;
            return this;
        }

        /**
         * @param cyclic 设置数据是否循环显示
         */
        public Builder setCyclic(boolean cyclic) {
            isCyclic = cyclic;
            return this;
        }

        /**
         * @param visibleItemNum 设置显示的可见日期数目
         * @return
         */
        public Builder setVisibleItemNum(int visibleItemNum) {
            this.visibleItemNum = visibleItemNum;
            return this;
        }

        /**
         * 设置当前小时：值为 0~23
         * 只在显示时分的方法里起作用
         *
         * @param curHour
         */
        public Builder setCurHour(int curHour) {
            this.curHour = curHour;
            return this;
        }

        /**
         * 设置当前分钟 值为 0~59
         * 只在显示时分的方法里起作用
         *
         * @param curMin
         */
        public Builder setCurMin(int curMin) {
            this.curMin = curMin;
            return this;
        }

        /**
         * 注意：当前时间既是最大时间也是最小时间没有意义，所以maxIsNow和minIsNow不要同时为true
         * 当maxIsNow为true时，设置minIsNow值则不起作用，以maxIsNow为准
         */
        public Builder setMaxIsNow(boolean maxIsNow) {
            this.maxIsNow = maxIsNow;
            return this;
        }

        /**
         * 注释同setMaxIsNow
         */
        public Builder setMinIsNow(boolean minIsNow) {
            this.minIsNow = minIsNow;
            return this;
        }
    }
}
